package com.gwkj.zxjl_user.HttpRequest;

import android.content.Context;

import com.gwkj.zxjl_user.AccountDB.ADBService;
import com.gwkj.zxjl_user.Bean.Other.DeviceEntity;
import com.gwkj.zxjl_user.SwitchOptions;
import com.gwkj.zxjl_user.Utils.AccessUtils;
import com.gwkj.zxjl_user.Utils.L;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;

import cz.msebera.android.httpclient.Header;

/**
 * Created by fujh on 16/4/1.
 */
public final class HttpRequestModelImpl implements HttpRequestModel{

    private AsyncHttpClient client;
    private Context context;

    public HttpRequestModelImpl(Context context){

        this.client = new AsyncHttpClient();
        this.context = context;
    }

    @Override
    public void request(HttpRequestListener listener,  HttpURL httpURL){
        this.request(listener, httpURL, null);
    }

    @Override
    public void request(HttpRequestListener listener, HttpURL httpURL, RequestParams params) {
        this.request(listener, httpURL.getKey(), httpURL.getStyle(), httpURL.getUrl(), params);
    }

    @Override
    public void request(final HttpRequestListener listener, final int key, String style, String url, RequestParams params) {

        TextHttpResponseHandler responseHandler = new TextHttpResponseHandler() {

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                L.e("SUCCESS", responseString + "");
                listener.onSuccessRequest(key, statusCode, headers, responseString);
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                L.e("FAILURE", responseString + "");
                listener.onFailureRequest(key, statusCode, headers, responseString, throwable);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                listener.onFinishedRequest(key);
            }
        };

        L.e("request", "url=" + pretraetURL(url) +", "+"params:"+ (params == null?"null":params.toString()));

        if (style.equals(HttpConstants.TYPE.GET)){

            client.get(pretraetURL(url), pretreatParams(params), responseHandler);
        }else if (style.equals(HttpConstants.TYPE.POST)){

            client.post(pretraetURL(url), pretreatParams(params), responseHandler);
        }else if (style.equals(HttpConstants.TYPE.PUT)){

            client.put(pretraetURL(url), pretreatParams(params), responseHandler);
        }else if (style.equals(HttpConstants.TYPE.DELETE)){

            client.delete(pretraetURL(url), pretreatParams(params), responseHandler);
        }else {
            L.e("ERROR", "请求方式有误");
        }
    }

    private String pretraetURL(String url){
        if (SwitchOptions.API.OUTSIDE){
            return HttpConstants.API.OUTSIDE_API + url;
        }else {
            return HttpConstants.API.INSIDE_API + url;
        }
    }

    private RequestParams pretreatParams(RequestParams params){
        if (params == null){
            params = new RequestParams();
        }

        ADBService adbService = new ADBService(context);
        DeviceEntity device = adbService.getDevice();
        params.add("platform", device.getPlatform());
        params.add("version", device.getVersion());
        params.add("app", device.getApp());
        params.add("device_id", device.getDevice_id());
        params.add("device_info", device.getDevice_info());

        String access_time = AccessUtils.getTime();
        params.add("access_time", access_time);
        params.add("access_token", AccessUtils.getToken(access_time));

        params.add("session_id", adbService.getToken());

        return params;
    }
}
